//
//  SRMeView.m
//  SportRing
//
//  Created by icelee on 14-11-1.
//  Copyright (c) 2014年 HotOne. All rights reserved.
//

#import "SRMeView.h"
#import "SRMe+Cate.h"
#import "SRDevice+Cate.h"


@interface SRMeView()<SRTableViewCellDelegate>

@property (nonatomic, weak)IBOutlet UITableView *srTableView;
@property (nonatomic, assign)int battery;

@end

@implementation SRMeView

- (void)awakeFromNib
{
    [super awakeFromNib];
    
    [self.srTableView setBackgroundColor:[UIColor colorWithRed:246.0/255.0 green:245.0/255.0 blue:240.0/255.0 alpha:1.0]];
}

- (void)setSrMeHeaderCellModel:(SRMeHeaderCellModel *)srMeHeaderCellModel
{
    if (_srMeHeaderCellModel == srMeHeaderCellModel) {
        return;
    }
    _srMeHeaderCellModel = srMeHeaderCellModel;
    
    [_srTableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:0 inSection:0]] withRowAnimation:UITableViewRowAnimationNone];
}

- (void)setSrTableViewCellModel:(SRTableViewCellModel *)srTableViewCellModel
{
    if (_srTableViewCellModel == srTableViewCellModel) {
        return;
    }
    _srTableViewCellModel = srTableViewCellModel;
    
    [_srTableView reloadData];
}

- (void)setBattery:(int)battery
{
    if (_battery == battery)
    {
        return;
    }
    _battery = battery;
    
    [_srTableView reloadRowsAtIndexPaths:@[[NSIndexPath indexPathForRow:1 inSection:2]] withRowAnimation:UITableViewRowAnimationNone];
}

#pragma mark - UITableViewDataSource ,UITableViewDelegate

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 7;
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    

    if (section == 2)
    {
        return 2;
    }
    else
    {
        return 1;
    }
    
    return 0;
    
}

-(UIView*)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section

{
    UIView *footerView = nil;
    
    if (section == 2)
    {
        footerView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.srTableView.frame.size.width, 15)];
    }
    else
    {
        footerView=[[UIView alloc] initWithFrame:CGRectMake(0, 0, self.srTableView.frame.size.width, 7)];
    }

    footerView.backgroundColor=[UIColor clearColor];
    
    return footerView;
    
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    if (section == 2)
    {
        return 15;
    }
    else
    {
        return 7;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0)
    {
        return 135;
    }
    else
    {
        return 49;
    }
}

// Row display. Implementers should *always* try to reuse cells by setting each cell's reuseIdentifier and querying for available reusable cells with dequeueReusableCellWithIdentifier:
// Cell gets various attributes set automatically based on table (separators) and data source (accessory views, editing controls)

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    SRTableViewCell *contentCell = [tableView dequeueReusableCellWithIdentifier:@"MeContentCell"];
    SRMeHeaderCell *firstCell = [tableView dequeueReusableCellWithIdentifier:@"MeHeaderCell"];
    
    if (indexPath.section == 0)
    {
        //TO DO: data
        [firstCell setSRMeHeaderCellData:_srMeHeaderCellModel];
        return firstCell;
    }
    else
    {
//        //显示和隐藏switch开关
//        if (indexPath.section == 4 || indexPath.section == 5)
//        {
//            if (indexPath.section == 4)
//            {
//                contentCell.swith.tag = SR_SWITCH_TYPE_PHONE;
//            }
//            if (indexPath.section == 5)
//            {
//                contentCell.swith.tag = SR_SWITCH_TYPE_SMS;
//            }
//            contentCell.accessoryView = nil;
//            contentCell.accessoryType = UITableViewCellAccessoryNone;
//            contentCell.srcDelegate = self;
//            [contentCell showSwitchButton];
//        }
//        else
//        {
            [contentCell hideSwitchButton];
            
//        }
        
        //section:1
        if (indexPath.section == 1)
        {
            
            [contentCell setSRTableViewCellData:[[SRTableViewCellModel alloc] initWithTipImage:[UIImage imageNamed:@"me_icon"] title:@"个人资料" content:nil switchValue:NO]];
        }
        
        //section：2
        else if (indexPath.section == 2)
        {
            if (indexPath.row == 0)
            {
                
                [contentCell setSRTableViewCellData:[[SRTableViewCellModel alloc] initWithTipImage:[UIImage imageNamed:@"me_hand_cycle_icon"] title:@"我的手环" content:nil switchValue:NO]];
            }
            if (indexPath.row == 1)
            {
                contentCell.contentLabel.textAlignment = NSTextAlignmentRight;
                contentCell.accessoryType = UITableViewCellAccessoryNone;
                [contentCell setSRTableViewCellData:[[SRTableViewCellModel alloc] initWithTipImage:[UIImage imageNamed:@"me_barrery_icon"] title:@"手环电量" content:[NSString stringWithFormat:@"%d%@",self.battery,@"%"] switchValue:NO]];
            }
            
        }
        
        else if (indexPath.section == 3)
        {
            contentCell.contentLabel.textAlignment = NSTextAlignmentLeft;
            [contentCell setSRTableViewCellData:[[SRTableViewCellModel alloc] initWithTipImage:[UIImage imageNamed:@"me_silence_clock_icon"] title:@"无声闹钟" content:(_srTableViewCellModel.content?_srTableViewCellModel.content:@"") switchValue:NO]];
        }
//        else if (indexPath.section == 4)
//        {
//            
//            [contentCell setSRTableViewCellData:[[SRTableViewCellModel alloc] initWithTipImage:[UIImage imageNamed:@"me_phone_icon"] title:@"来电提醒" content:nil switchValue:_srTableViewCellModel.swichValue]];
//        }
//        else if (indexPath.section == 5)
//        {
//            
//            [contentCell setSRTableViewCellData:[[SRTableViewCellModel alloc] initWithTipImage:[UIImage imageNamed:@"me_sms_icon"] title:@"短息提醒" content:nil switchValue:_srTableViewCellModel.swichValue]];
//        }
        
        else if (indexPath.section == 4)
        {
            
            [contentCell setSRTableViewCellData:[[SRTableViewCellModel alloc] initWithTipImage:[UIImage imageNamed:@"me_comment_icon"] title:@"我要吐槽" content:nil switchValue:NO]];
        }
        else if (indexPath.section == 5)
        {
            
            [contentCell setSRTableViewCellData:[[SRTableViewCellModel alloc] initWithTipImage:[UIImage imageNamed:@"me_about_icon"] title:@"关于" content:nil switchValue:NO]];
        }
        else if (indexPath.section == 6)
        {
            contentCell.accessoryType = UITableViewCellAccessoryNone;
            contentCell.titleLabel.textAlignment = NSTextAlignmentCenter;
            contentCell.titleLabel.font = [UIFont systemFontOfSize:13];
            contentCell.titleLabel.textColor = [UIColor clearColor];
            contentCell.titleLabel.text = @"退出登录";
            contentCell.contentLabel.hidden = YES;
            contentCell.tipImageView.hidden = YES;
        }
        else
        {
            
        }
        
        return contentCell;
        
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 1)
    {
        if (_srMeDelegate && [_srMeDelegate respondsToSelector:@selector(gotoPersonalDataViewController)])
        {
            [_srMeDelegate gotoPersonalDataViewController];
        }
        
    }
    if (indexPath.section == 2)
    {
        if (indexPath.row == 0)
        {
            if (_srMeDelegate && [_srMeDelegate respondsToSelector:@selector(gotoMyHandRingViewController)])
            {
                [_srMeDelegate gotoMyHandRingViewController];
            }
            
        }
        
    }
    if (indexPath.section == 3)
    {
        if (_srMeDelegate && [_srMeDelegate respondsToSelector:@selector(gotoSilenceClockViewController)])
        {
            [_srMeDelegate gotoSilenceClockViewController];
        }
        
    }
    if (indexPath.section == 4)
    {
        if (_srMeDelegate && [_srMeDelegate respondsToSelector:@selector(gotoCommentViewController)])
        {
            [_srMeDelegate gotoCommentViewController];
        }
        
    }
    if (indexPath.section == 5)
    {
        if (_srMeDelegate && [_srMeDelegate respondsToSelector:@selector(gotoAboutViewController)])
        {
            [_srMeDelegate gotoAboutViewController];
        }
        
    }
    if (indexPath.section == 6) {
        
        if (_srMeDelegate && [_srMeDelegate respondsToSelector:@selector(quitLogin)])
        {
            [_srMeDelegate quitLogin];
        }
    }
    
}

#pragma mark - SRTableViewCellDelegate

-(void)switchValue:(BOOL)value type:(SR_SWITCH_TYPE)type
{
    if (_srMeDelegate && [_srMeDelegate respondsToSelector:@selector(switchValue:type:)])
    {
        [_srMeDelegate switchValue:value type:type];
    }
    
}


/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
